package game.views;

import hx.events.MouseEvent;
import game.script.AiScript;
import game.event.WorldEvent;
import hx.events.Event;
import hx.events.Keyboard;
import hx.events.KeyboardEvent;
import game.worlds.IStateView;
import motion.Actuate;
import game.worlds.Player2Key;
import game.worlds.Player1Key;
import game.worlds.GameRole;
import game.worlds.GameWorld;
import hx.display.Scene;

/**
 * 战斗界面状态栏
 */
@:build(hx.macro.UIBuilder.build("assets/views/FightStateView.xml"))
class FightStateView extends Scene implements IStateView {
	/**
	 * 当前游戏时间
	 */
	public var world:GameWorld;

	/**
	 * 设置游戏超时设定，当超过超时设定时，游戏将结束，如果为0，则为无限时间
	 */
	public var timeout:Int = 0;

	public function new(world:GameWorld) {
		this.world = world;
		super();
	}

	override function onInit() {
		super.onInit();
		this.test.visible = false;
		this.updateEnabled = true;
		this.time.horizontalAlign = CENTER;
		// 隐藏连击容器
		this.p1combo.visible = this.p2combo.visible = false;
		// 头像渲染
		this.p1.roleNameLabel.data = "？？？";
		this.p2.roleNameLabel.data = "？？？";
		this.debug.visible = false;
		// 侦听胜利消息
		this.addEventListener("winState", onWinState);
		this.menu.addEventListener(MouseEvent.CLICK, function(e:MouseEvent) {
			// 打开暂停界面
			this.openPauseView();
		});
	}

	/**
	 * 打开暂停界面
	 */
	private function openPauseView():Void {
		if (this.world.paused)
			return;
		this.world.pauseGame();
		var view = new PauseView();
		this.topView.addChild(view);
		view.onReleaseEvent.add(this.world.continueGame);
	}

	private function onWinState(e:Event):Void {
		var role:GameRole = e.data;
		if (role.troop == 0) {
			this.p1.winTimes++;
		} else {
			this.p2.winTimes++;
		}
	}

	override function onAddToStage() {
		super.onAddToStage();
		// 隐藏无限时间
		this.mugen_time.visible = timeout <= 0;
		this.time.visible = !this.mugen_time.visible;
		this.stage.addEventListener(KeyboardEvent.KEY_DOWN, this.onKeyDown);
	}

	override function onRemoveToStage() {
		super.onRemoveToStage();
		this.stage.removeEventListener(KeyboardEvent.KEY_DOWN, this.onKeyDown);
	}

	private function onKeyDown(e:KeyboardEvent):Void {
		if (e.keyCode == Keyboard.ENTER) {
			this.openPauseView();
			return;
		}
		if (!this.debug.visible)
			return;
		switch (e.keyCode) {
			case Keyboard.C:
				// 恢复所有玩家的技能
				this.p1.role.cdData.clearAll();
				this.p2.role.cdData.clearAll();
			case Keyboard.V:
				// 恢复所有玩家的血量
				this.p1.role.data.hp = this.p1.role.data.maxHp;
				this.p1.role.data.mp = this.p1.role.data.maxMp;
				this.p2.role.data.hp = this.p2.role.data.maxHp;
				this.p2.role.data.mp = this.p2.role.data.maxMp;
				this.p1.setProgress(1);
				this.p2.setProgress(1);
			case Keyboard.B:
				// 碰撞线条
				this.p1.role.world.debug = !this.p1.role.world.debug;
			case Keyboard.N:
				// 开关敌人战斗逻辑
				if (this.p2.role.aiScript == null) {
					this.p2.role.aiScript = new AiScript();
				}
				this.p2.role.aiScript.enable = !this.p2.role.aiScript.enable;
		}
	}

	public function setDebug(debug:Bool):Void {
		this.debug.visible = debug;
	}

	override function onUpdate(dt:Float) {
		super.onUpdate(dt);
		// 计算剩余时间
		if (timeout > 0) {
			var time = Math.max(0, Std.int(timeout - this.world.worldRunTime));
			this.time.data = Std.string(time);
			if (time == 0 && !isTimeOver) {
				isTimeOver = true;
				this.world.dispatchEvent(new WorldEvent(WorldEvent.TIME_OVER));
			}
		}
	}

	/**
	 * 展示击中连击数
	 * @param role 
	 */
	public function showComboHit(role:GameRole):Void {
		if (role.keyReceiver == null)
			return;
		if (role.keyReceiver is Player1Key) {
			if (role.data.hitCounts > 0) {
				// 玩家1
				this.p1combo.visible = true;
				this.p1combo.alpha = 1;
				this.p1comboCounts.data = Std.string(role.data.hitCounts);
				this.p1comboCounts.scaleY = this.p1comboCounts.scaleX = 1.35;
				Actuate.tween(this.p1comboCounts, 0.5, {scaleX: 1, scaleY: 1});
				Actuate.tween(this.p1combo, 0.5, {alpha: 0}).autoVisible().delay(1);
			}
		} else if (role.keyReceiver is Player2Key) {
			// 玩家2
			if (role.data.hitCounts > 0) {
				this.p2combo.visible = true;
				this.p2combo.alpha = 1;
				this.p2comboCounts.data = Std.string(role.data.hitCounts);
				this.p2comboCounts.scaleY = this.p2comboCounts.scaleX = 1.35;
				Actuate.tween(this.p2comboCounts, 0.5, {scaleX: 1, scaleY: 1});
				Actuate.tween(this.p2combo, 0.5, {alpha: 0}).autoVisible().delay(1);
			}
		}
	}

	/**
	 * 是否已经时间超时
	 */
	public var isTimeOver = false;

	public function restart():Void {
		// 重新开始
		isTimeOver = false;
	}
}
